package net.lucamasini.ricette.server;

import static net.lucamasini.ricette.server.EntityManagerFactoryListener.jpaTemplateInContext;

import java.io.IOException;
import java.io.PrintWriter;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lucamasini.ricette.model.*;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		if (req.getParameter("creaIngredienti") != null) {
			creaIngredienti(resp, "Spinaci", "Verdure", "Contorno");
		} else if (req.getParameter("cancellaIngredienti") != null) {
			cancellaIngredienti(resp);
		} else if (req.getParameter("creaRicetta") != null) {
			creaRicette(resp);
		} else if (req.getParameter("creaTipoIngrediente") != null) {
			creaTipoIngrediente(resp, req.getParameter("tipoIngrediente"));
		} else {
			resp.getWriter()
					.println(
							"nessun parametro tra quelli disponibili specificato (creaIngredienti, cancellaIngredienti, testManyToOne, creaRicetta)");
		}

	}

	private void creaIngredienti(HttpServletResponse resp, String ig, String tipoIngrediente, String famiglia)
			throws ServletException {
		try {
			JpaTemplate template = jpaTemplateInContext(getServletContext());

			TipoIngrediente ti = new TipoIngrediente();
			ti.setDescrizione(tipoIngrediente);
			
			template.persist(ti);
			
			Ingrediente ingrediente = new Ingrediente();
			ingrediente.setDescrizione(ig);
			ingrediente.setCalorie(100);
			ingrediente.setGlutine(false);
			ingrediente.setKeyTipoIngrediente(ti.getId());

			template.persist(ingrediente);

			stampa(resp, ingrediente);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

	private void creaTipoIngrediente(HttpServletResponse resp,
			String tipoIngrediente) throws ServletException {
		try {
			JpaTemplate template = jpaTemplateInContext(getServletContext());

			TipoIngrediente ti = new TipoIngrediente();
			ti.setDescrizione(tipoIngrediente);

			template.persist(ti);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
	
	private void creaRicette(HttpServletResponse resp) throws ServletException {
		try {
			JpaTemplate template = jpaTemplateInContext(getServletContext());

			Ingrediente ingrediente = new Ingrediente();

			Ricetta ricetta1 = creaRicetta(ingrediente);
			Ricetta ricetta2 = creaRicetta(ingrediente);

			template.persist(ricetta1);
			template.persist(ricetta2);

			stampa(resp, ricetta1);
			stampa(resp, ricetta2);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

	private void cancellaIngredienti(final HttpServletResponse resp)
			throws ServletException {
		try {
			JpaTemplate template = jpaTemplateInContext(getServletContext());

			final PrintWriter writer = resp.getWriter();
			template.execute(new JpaCallback<Void>() {

				@Override
				public Void doInJpa(EntityManager em)
						throws PersistenceException {

					writer.print("result: "
							+ em.createQuery("delete from Ingrediente")
									.executeUpdate());

					return null;
				}
			});
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

	private void stampa(HttpServletResponse resp, Ricetta ricetta1)
			throws IOException {
		resp.getWriter().println(ricetta1.getId());
		for (IngredientiRicetta ir : ricetta1.getIngredientiRicetta()) {
			resp.getWriter().println(ir.getId());
		}
	}

	private void stampa(HttpServletResponse resp, Ingrediente ingrediente) throws IOException {
		if( ingrediente!=null ){
			resp.getWriter().println(ingrediente.getId());
			resp.getWriter().println(ingrediente.getKeyTipoIngrediente());
			resp.getWriter().println(ingrediente.getDescrizione());
		} else {
			resp.getWriter().println("Ingrediente null !!!");
		}
	}

	private Ricetta creaRicetta(Ingrediente ingrediente) {
		IngredientiRicetta ingredientiRicetta = new IngredientiRicetta();
		ingredientiRicetta.setKeyIngrediente(ingrediente.getId());

		Ricetta ricetta1 = new Ricetta();
		ricetta1.getIngredientiRicetta().add(ingredientiRicetta);
		return ricetta1;
	}
}
